﻿namespace _13_装饰器模式;

public class Other
{

}

// Component 接口
public interface IComponent
{
    void Operation();
}

// ConcreteComponent 具体组件类
public class ConcreteComponent : IComponent
{
    public void Operation()
    {
        Console.WriteLine("ConcreteComponent operation");
    }
}

// Decorator 装饰者抽象类
public abstract class Decorator : IComponent
{
    protected IComponent component;

    protected Decorator(IComponent component)
    {
        this.component = component;
    }

    public virtual void Operation()
    {
        component.Operation();
    }
}

// ConcreteDecorator 具体装饰者类
public class ConcreteDecoratorA : Decorator
{
    public ConcreteDecoratorA(IComponent component) : base(component) { }

    public override void Operation()
    {
        base.Operation();
        AddedBehavior();
        Console.WriteLine("ConcreteDecoratorA operation");
    }

    private void AddedBehavior()
    {
        Console.WriteLine("Added behavior for ConcreteDecoratorA");
    }
}

// 另一个具体装饰者类
public class ConcreteDecoratorB : Decorator
{
    public ConcreteDecoratorB(IComponent component) : base(component) { }

    public override void Operation()
    {
        base.Operation();
        AddedBehavior();
        Console.WriteLine("ConcreteDecoratorB operation");
    }

    private void AddedBehavior()
    {
        Console.WriteLine("Added behavior for ConcreteDecoratorB");
    }
}

class Program
{
    static void Main()
    {
        // 创建一个具体组件
        IComponent component = new ConcreteComponent();

        // 使用装饰者A包装组件
        Decorator decoratorA = new ConcreteDecoratorA(component);
        decoratorA.Operation();

        Console.WriteLine();

        // 使用装饰者B包装组件
        Decorator decoratorB = new ConcreteDecoratorB(component);
        decoratorB.Operation();
    }
}
